package com.example.douyin.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.douyin.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class  FirstPageFragment extends Fragment {
    TabLayout tabLayout;
    ViewPager viewPager;
    List<Fragment>fragmentList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first_page, container, false);
        tabLayout = view.findViewById(R.id.main_tab);
        viewPager = view.findViewById(R.id.main_vp);
        tabLayout.setupWithViewPager(viewPager);

        //加载页面
        initPager();
        return view;
    }

    private void initPager() {
        fragmentList = new ArrayList<>();
        //加载viewpager的页面
        String[] url_array = URLUtils.getUrl_array();
        String[] title_array = URLUtils.getTitle_array();
        for (int i = 0; i < url_array.length; i++) {
            NewsFragment frag = NewsFragment.newInstance(url_array[i],title_array[i]);
            fragmentList.add(frag);
        }
        //设置适配器
        NewsPagerAdapter adapter = new NewsPagerAdapter(getChildFragmentManager(), fragmentList, title_array);
        viewPager.setAdapter(adapter);
    }
}